import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter_base/common/utils/utils.dart';
import 'package:flutter_base/common/values/values.dart';
import 'package:flutter/material.dart';

/// 缓存图片
Widget imageCached(String url,
    {double width = 48,
    double height = 48,
    EdgeInsetsGeometry? margin,
    String format: 'png'}) {
  // if (format == 'svg') {
  //   return SvgPicture.network(
  //     url,
  //     height: zhiSetRadius(height),
  //     width: zhiSetRadius(width),
  //     placeholderBuilder: (BuildContext context) => Container(
  //       height: zhiSetHeight(height),
  //       width: zhiSetWidth(width),
  //       // alignment: Alignment.center,
  //       // child: CircularProgressIndicator(),
  //     ),
  //   );
  // }
  return CachedNetworkImage(
    imageUrl: url,
    imageBuilder: (context, imageProvider) => Container(
      height: zhiSetRadius(height),
      width: zhiSetRadius(width),
      margin: margin,
      decoration: BoxDecoration(
        borderRadius: Radii.k6pxRadius,
        image: DecorationImage(
          image: imageProvider,
          fit: BoxFit.cover,
          // colorFilter: ColorFilter.mode(Colors.red, BlendMode.colorBurn),
        ),
      ),
    ),
    placeholder: (context, url) {
      return Container(
        height: zhiSetRadius(height),
        width: zhiSetRadius(width),
        margin: margin,
      );
    },
    errorWidget: (context, url, error) => Icon(Icons.error),
  );
}

/// 加载本地资源图片
Widget loadAssetImage(String? name,
    {double? width, double? height, BoxFit? fit, String format: 'png'}) {
  return Image.asset(
    getImgPath(name, format: format),
    height: height,
    width: width,
    fit: fit,
  );
}

String getImgPath(String? name, {String format: 'png'}) {
  return 'assets/images/$name.$format';
}

String getNetWorkImgPath(String? url) {
  return '$NETWORK_URL$url';
}

// 加载网络图片
Widget loadNetworkImage(String imageUrl,
    {String placeholder: "none",
    double? width,
    double? height,
    BoxFit fit: BoxFit.cover}) {
  print(imageUrl);
  return CachedNetworkImage(
    imageUrl: imageUrl == null ? "" : imageUrl,
    placeholder: (context, url) =>
        loadAssetImage(placeholder, height: height, width: width, fit: fit),
    errorWidget: (context, url, error) =>
        loadAssetImage(placeholder, height: height, width: width, fit: fit),
    width: width,
    height: height,
    fit: fit,
  );
}
